from django.db import connection
from django.http import HttpResponse
from django.shortcuts import render
from .models import *
from pymysql import Connect


# Create your views here.


def index(request):
    """测试原生sql语句"""
    # 【一】使用Django 管理器 执行原生sql语句
    # rqs = BookInfo.objects.raw('select * from tb_books')
    # for item in rqs:
    #     #item : BookInfo对象
    #     print(item,type(item))
    #     print(item.bread)

    # 【二】使用Python执行原生sql语句
    # conn=Connect(user='root',password='1234',port=3306,host='127.0.0.1',database='db_books')
    # cur=conn.cursor()
    # cur.execute('select * from tb_heros')
    # for row in cur.fetchall():
    #     print(row)
    # cur.close()
    # conn.close()

    # 【三】使用Python+Django执行原生sql语句
    with connection.cursor() as cur:
        cur.execute('select * from tb_heros where id<5')
        row=cur.fetchone()
        print(row)
    return HttpResponse('原生SQL测试...')
